package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.model.bo.CommentReplyBo;
import com.cskaoyan.catmall.model.bo.OrderBo;
import com.cskaoyan.catmall.model.bo.OrderIdBo;
import com.cskaoyan.catmall.model.bo.RefundBo;
import com.cskaoyan.catmall.model.bo.ShipBo;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.ChannelVo;
import com.cskaoyan.catmall.model.vo.OrderDetailVo;
import com.cskaoyan.catmall.model.vo.OrderVo;
import com.cskaoyan.catmall.service.CommentService;
import com.cskaoyan.catmall.service.OrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

/**
 * @Author: 逍遥子
 * @Date: 2022/01/07/18:23
 * @Description:
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    CommentService commentService;
    /**
     * @param
     * @param orderBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 订单列表
     * @author czx
     * @date 2022-01-08 9:19
     */
    @GetMapping("list")
    public BaseRespVo list(OrderBo orderBo) {
        OrderVo orderVo = orderService.selectOrder(orderBo);
        return BaseRespVo.ok(orderVo);
    }

    /**
     * @param
     * @param id
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 订单详情
     * @author czx
     * @date 2022-01-08 9:20
     */
    @GetMapping("detail")
    public BaseRespVo detail(Integer id) {

        OrderDetailVo orderDetailVo = orderService.selectOrderDetailById(id);


        if(orderDetailVo.getOrder() == null){
            return BaseRespVo.ok();
        }else{

            return BaseRespVo.ok(orderDetailVo);
        }

    }

    /**
     * @param
     * @param shipBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 发货
     * @author czx
     * @date 2022-01-08 9:20
     */
    @PostMapping("ship")
    public BaseRespVo ship(@RequestBody ShipBo shipBo) {

        System.out.println(shipBo.getOrderId());

        int res = orderService.updateOrderStatus(shipBo);

        if (res == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail();
        }
    }

    /**
     * @description 订单退款
     * @author czx
     * @date 2022-01-09 17:03
     * @param
     * @param refundBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @PostMapping("refund")
    public BaseRespVo refund(@RequestBody RefundBo refundBo){
        int res = orderService.OrderRefund(refundBo);
        if(res == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }
    /**
     * @description 获取所有快递信息
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @author czx
     * @date 2022-01-08 9:47
     */
    @GetMapping("channel")
    public BaseRespVo channel() {

        ArrayList<ChannelVo> arr = new ArrayList<>();

        ChannelVo channelVo1 = new ChannelVo("ZTO", "中通快递");
        ChannelVo channelVo2 = new ChannelVo("YTO", "圆通速递");
        ChannelVo channelVo3 = new ChannelVo("YD", "韵达速递");
        ChannelVo channelVo4 = new ChannelVo("YZPY", "邮政快递包裹");
        ChannelVo channelVo5 = new ChannelVo("DBL", "德邦快递");
        ChannelVo channelVo6 = new ChannelVo("EMS", "EMS");

        arr.add(channelVo1);
        arr.add(channelVo2);
        arr.add(channelVo3);
        arr.add(channelVo4);
        arr.add(channelVo5);
        arr.add(channelVo6);

        return BaseRespVo.ok(arr);
    }

    /**
     * @description 订单回复
     * @author ghr
     * @date 2022/1/10 1:00 下午
     * @param
     * @param commentReplyBo
     * @param bindingResult
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @PostMapping("reply")
    public BaseRespVo replyComment(@Validated @RequestBody CommentReplyBo commentReplyBo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return BaseRespVo.fail("参数不能为空");
        }
        Integer commentId = commentReplyBo.getCommentId();
        int code = commentService.selectDuplicateComment(commentId);
        if (code > 0){
            return BaseRespVo.fail("订单商品已回复");
        }
        commentService.updateComment(commentReplyBo);
        return BaseRespVo.ok();
    }

    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 删除订单
     * @author czx
     * @date 2022-01-08 10:36
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody OrderIdBo orderIdBo) {

        int res = orderService.deleteOrder(orderIdBo.getOrderId());

        if (res == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail();
        }

    }
}
